import 'package:aichat_flutter_wallet/module_common/widgets/button/public_button.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/image/public_image.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/page_status_com.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_database/lib_database.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../../module_common/utils/colors.dart';


class FansPageList extends StatefulWidget {
  final List<FansTabsItemDataType> list;
  final Function onClick;
  final Function getMore;
  final int pageStatus;
  final String title;

  // final List<int> list;
  final bool hasMore;

  const FansPageList(
      {super.key,
        required this.list,
        required this.pageStatus,
        required this.title,
        this.hasMore = false,
        required this.onClick,
        required this.getMore,
      });

  @override
  State<FansPageList> createState() => _fansPageListState();
}

class _fansPageListState extends State<FansPageList> {
  List<FansTabsItemDataType> get list => widget.list;

  bool get hasMore => widget.hasMore;

  RefreshController _refreshController =
  RefreshController(initialRefresh: false);

  int get pageStatus => widget.pageStatus;
  String get title => widget.title;

  bool btnStatus = false;

  String btnText(FansTabsItemDataType item) {
    int status= item.status;
    if (status == 0) {
      return 'Follow';
    }
    if(status == 1){
      return 'Followed';
    }
    return 'Mutual attention';
  }

  ButtonType btnType(int status) {
    if ([1,2].contains(status)) {
      return ButtonType.defaultType;
    }
    return ButtonType.private;
  }

  void _onLoading() {
    widget.getMore().then((_){
      _refreshController.loadComplete();
    });
  }

  Widget FansItem(FansTabsItemDataType item, int index) {
    return Container(
      margin: EdgeInsets.only(top: index != 0 ? 0.w : 15.w, bottom: 10.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10.w),
        border: Border.all(
          color: Colors.transparent,
          width: 0.w,
        ),
      ),
      padding: EdgeInsets.symmetric(vertical: 15.w, horizontal: 0.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          ClipRRect(
            borderRadius: BorderRadius.circular(40),
            child: PublicImage(
              image: item.icon,
              height: 40.w,
              width: 40.w,
            ),
          ),
          SizedBox(
            width: 10.w,
          ),
          Expanded(
              child: Container(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Container(
                        constraints: BoxConstraints(
                          maxWidth: 100.w,
                        ),
                        child: Text(
                          item.title,
                          softWrap: false,
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                              fontSize: 14.sp,
                              fontWeight: FontWeight.w400,
                              color: Color.fromRGBO(51, 51, 51, 1)),
                        ),
                      ),
                      SizedBox(width: 7.w,),
                      Visibility(
                        visible: item.isFriend,
                          child: PublicImage(image: 'assets/images/wallet/is_firend.png',width: 51.w,height: 16.w,))
                    ],
                  ),
                  SizedBox(
                    height: 3.w,
                  ),
                  RichText(
                    softWrap: false,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    text: TextSpan(
                      text: item.describe,
                      style: TextStyle(
                          color: Color.fromRGBO(190, 189, 202, 1),
                          fontSize: 12.sp,
                          fontWeight: FontWeight.w400),
                      children: [
                        // TextSpan(
                        //     text: '+${item.title.toString()} KC',
                        //     style: TextStyle(
                        //         color: Color.fromRGBO(134, 103, 254, 1),
                        //         fontSize: 12.sp,
                        //         fontWeight: FontWeight.w400)),
                      ],
                    ),
                  ),


                ],
              ))),
          Visibility(
            visible: true, //特殊处理，如果是签到任务，那就不用显示去完成的按钮
            child: Container(
              height: 30.w,
              constraints: BoxConstraints(
                maxWidth: item.status==2?110.w:80.w,
              ),
              child: PublicButton(
                text: btnText(item),
                margin: EdgeInsets.zero,
                type: btnType(item.status),
                disabledStatus: item.status == 1 || item.status == 2,
                onClick: () async {
                  if (btnStatus) {
                    return;
                  }
                  btnStatus = true;
                  await widget.onClick(item,index);
                  btnStatus = false;
                },
              ),
            ),
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    // super.build(context);
    return Container(
      margin: EdgeInsets.only(left: 20.w, right: 20.w),
      child: SmartRefresher(
        enablePullUp: true,
        enablePullDown: false,
        onLoading: _onLoading,
        controller: _refreshController,
        footer: CustomFooter(
          builder: (BuildContext ctx,LoadStatus? mode){
            Widget body;
            if(mode==LoadStatus.idle){
              body = Text(
                'Already at the bottom',
                textAlign: TextAlign.center,
                style: TextStyle(color: AppColors.c_35355B, fontSize: 12.sp, fontWeight: FontWeight.w400),
              );
            }
            else if(mode==LoadStatus.loading){
              body =  CupertinoActivityIndicator();
            }
            else if(mode == LoadStatus.failed){
              body = Text("Load Failed!Click retry!",textAlign: TextAlign.center,
                style: TextStyle(color: AppColors.c_35355B, fontSize: 12.sp, fontWeight: FontWeight.w400),);
            }
            else if(mode == LoadStatus.canLoading){
              body = Text("release to load more",textAlign: TextAlign.center,
                style: TextStyle(color: AppColors.c_35355B, fontSize: 12.sp, fontWeight: FontWeight.w400),);
            }
            else{
              body = Text(
                'Already at the bottom',
                textAlign: TextAlign.center,
                style: TextStyle(color: AppColors.c_35355B, fontSize: 12.sp, fontWeight: FontWeight.w400),
              );
            }
            return Container(
              height: 22.w,
              child: Center(child:body),
            );
          },
        ),
        child: ListView.builder(
            padding: EdgeInsets.only(top: 0, bottom: 0),
            physics: const ClampingScrollPhysics(),
            itemCount: list.isEmpty?1:list.length,
            itemBuilder: (context, index) {
              if (list.isEmpty) {
                return Container(
                  padding: EdgeInsets.only(top: 0.w),
                  color: Colors.transparent,
                  child: PageEmptyCom(title: 'Empty data'),
                );
              }
              // if (index == list.length) {
              //   return Container(
              //     padding: EdgeInsets.only(top: 10.w, bottom: 10.w),
              //     alignment: Alignment.center,
              //     child: Text(
              //       'Already at the bottom',
              //       style: TextStyle(
              //           fontSize: 12.sp,
              //           color: Color.fromRGBO(190, 189, 202, 1),
              //           fontWeight: FontWeight.w400),
              //     ),
              //   );
              // }
              var item = list[index];
              return FansItem(item, index);
            })
      )


  ,
    );
  }
}
